<?php
namespace Admin\Controller;
use Think\Controller;
class LogController extends CommonController {
    public function index()
    {
        //分页显示
        $admin_log = M('admin_history'); // 实例化对象
        $count      = $admin_log->count();// 查询满足要求的总记录数
        $this->assign('count',$count);
        $Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $logs = $admin_log->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('logs',$logs);
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }

    //批量删除
    public function deletes()
    {
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);

        //删除
        $res = M('admin_history')->delete($ids);
          if ($res) {
              $arr = array(
                  'error' =>1,
                  'info'=>"批量删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>0,
                  'info'=>"批量删除失败"
                  );
              echo json_encode($arr);
          }
      }
    }

    //删除
    public function delete()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('admin_history')->delete($id);
            if ($res) {
                $arr = array(
                    'error' => 1 ,
                    'info' => '删除成功'
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0 ,
                    'info' => '删除失败'
                    );
                echo json_encode($arr);
            }
        }
    }

    //问题
    public function problem()
    {
      $pros = M('gy_problems')->select();
      $this->assign('pros',$pros);

      $this->display();
    }
    //添加
    public function proadd()
    {
      if (IS_POST) {
        $data['title'] = I('post.title');
        $data['content'] = I('post.content');
        $data['time'] = time();
        $res = M('gy_problems')->add($data);
        if ($res) {
            $arr = array(
                'error' => 1 ,
                'info' => '添加成功'
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' => 0 ,
                'info' => '添加失败'
                );
            echo json_encode($arr);
        }
        
      }else {
        $this->display();
      }
    }
    //编辑
    public function proedit()
    {
      if (IS_POST) {
        $id = I('post.id');
        $data['title'] = I('post.title');
        $data['content'] = I('post.content');
        $data['time'] = time();

        $res = M('gy_problems')->where(['id'=>$id])->save($data);
        if ($res) {
            $arr = array(
                'error' => 1 ,
                'info' => '编辑成功'
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' => 0 ,
                'info' => '编辑失败'
                );
            echo json_encode($arr);
        }

      }else {
        $id = I('get.id');
        $pro = M('gy_problems')->where(['id'=>$id])->find();
        $this->assign('pro',$pro);
        $this->display();
      }
    }










}